
// Profiler.h
//

#pragma once

#include <BaseDefine.h>

typedef struct {
	bool bValid;		                 //Whether this data is valid
	unsigned int iProfileInstances;      //# of times ProfileBegin called
	int iOpenProfiles;			         //# of times ProfileBegin w/o ProfileEnd
	char szName[256];					 //Name of sample
	float fStartTime;					//The current open profile start time
	float fAccumulator;					//All samples this frame added together
	float fChildrenSampleTime;			//Time taken by all children
	
	unsigned int iNumParents;           //Number of profile parents	
	int parentsNum;
} ProfileSample;

typedef struct {
	bool bValid;        //Whether the data is valid
	char szName[256];   //Name of the sample
	float fAve;         //Average time per frame (percentage)
	float fMin;         //Minimum time per frame (percentage)
	float fMax;         //Maximum time per frame (percentage)
} ProfileSampleHistory;

/**
* @class Profiler
*  - 
*/
class Profiler{
	enum{
		NUM_PROFILE_SAMPLES=	50,
	};

public:
	void initialize();
	void writeResult();

	void startProfile(char *);
	void endProfile(char * );

	Profiler();
	~Profiler();

protected:

private:
	void storeProfileInHistory( char* name, float percent );
	void getProfileFromHistory( char* name, float* ave, float* min, float* max );

	float m_startProfile;
	float m_endProfile;	

	ProfileSample        m_samples[NUM_PROFILE_SAMPLES];
	ProfileSampleHistory m_history[NUM_PROFILE_SAMPLES];
};

/* 
g_Profile.InitProfile(LOG_TYPE_FILE, "PROFILE", "PROFILE");

while(g_blRunMainThread){
	g_Profile.StartProfile("WHILE");

	g_Profile.StartProfile("CycleTIME");
	pCycle->AdjustCycle();	
	pCycleTimer->ProcessTimer();
	g_Profile.EndProfile("CycleTIME");
		
	__try{
		g_Profile.StartProfile("SessionLOOP");
		pDelObj=pSessionMgr->SessionMgrLoop();
		pUserMgr->DisconnectObjProcess(pDelObj);
		g_Profile.EndProfile("SessionLOOP");

		g_Profile.StartProfile("PROCESS");
		pMSGProcessor->ProcessMSG();
		g_Profile.EndProfile("PROCESS");
	}
	__except(GetExceptionCode() != EXCEPTION_BREAKPOINT || GetExceptionCode() != EXCEPTION_SINGLE_STEP ){			
	}		

	g_Profile.EndProfile("WHILE");

	g_Profile.WriteResult();	
}	
*/
